
package com.eyedog.video.maincategory;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.eyedog.library.base.BaseFragment;
import com.eyedog.uilibrary.base.BaseFragmentPagerAdapter;
import com.eyedog.video.R;
import com.eyedog.video.entity.CategoryEntity;
import com.eyedog.video.videolist.VideoListFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

public class CategoryFragment extends BaseFragment implements constract.ICategoryView {

    ViewPager mPager;

    MagicIndicator mIndicator;

    BaseFragmentPagerAdapter mPagerAdapter;

    CategoryPresenter mPresenter;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.layout_main, container, false);
        return layout;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPager = (ViewPager) view.findViewById(R.id.view_pager);
        mIndicator = (MagicIndicator) view.findViewById(R.id.magic_indicator1);
        mPagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        mPresenter = new CategoryPresenter(this, CategoryInstance.getInstance());
        mPresenter.displayCategory();
    }

    private void initMagicIndicator3(final List<CategoryEntity> categoryEntities) {
        MagicIndicator magicIndicator = mIndicator;
        magicIndicator.setBackgroundColor(getResources().getColor(R.color.tab_color));
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return categoryEntities.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(
                    context);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.half_text));
                simplePagerTitleView.setSelectedColor(
                    getResources().getColor(R.color.tab_text_color));
                simplePagerTitleView.setText(categoryEntities.get(index).name);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setColors(getResources().getColor(R.color.tab_text_color));
                return linePagerIndicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, mPager);
    }

    @Override
    public void showCategories(final List<CategoryEntity> categoryEntities) {
        mPager.post(new Runnable() {
            @Override
            public void run() {
                if (categoryEntities != null && categoryEntities.size() > 0) {

                    initMagicIndicator3(categoryEntities);
                    List<VideoListFragment> fragments = new ArrayList<>();

                    for (CategoryEntity categoryEntity : categoryEntities) {
                        VideoListFragment fragment = new VideoListFragment();
                        fragment.setArguments(
                            VideoListFragment.createVideoListBundle(categoryEntity.id));
                        fragments.add(fragment);
                    }
                    mPagerAdapter.addFragments(fragments);
                    mPagerAdapter.notifyDataSetChanged();
                } else {
                    showFailedView("empty categorys");
                }
            }
        });
    }

    @Override
    public void showFailedView(final String err) {
        mPager.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getActivity(), err, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
